<?php

namespace app\common\model\order;

use app\admin\model\course\Order;
use app\admin\model\finance\Recharge;
use app\admin\model\user\Level;
use app\common\model\MoneyLog;
use app\common\model\User;
use think\Cache;
use think\Model;

/**
 * 支付日志
 */
class PayLog extends Model
{
    // 表名
    protected $name = 'pay_log';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    //未支付
    const STATUS_UN_PAY = 1;
    const STATUS_PAY = 2;
    const STATUS_REFUND = -100;

    //课程订单
    const COURSE_ORDER = "CO";
    //充值余额
    const RECHARGE_MONEY = "RE";
    //充值会员
    const SUPER_VIP = "SP";
    //购买优惠券
    const BUY_COUPON = "BC";
    //退款
    const REFUND = "RE";
    //创建支付单
    public function createPayLog(User $user, $order_sn, $total_fee, $pay_from, $pay_type, $extend = []){
        $data = [
            "user_id" => $user["id"],
            "order_sn" => $order_sn,
            "total_fee" =>$total_fee,
            "status" => self::STATUS_UN_PAY,
            "pay_from" => $pay_from,
            "pay_type" => $pay_type,
        ];
        $data = array_merge($data, $extend);
        $this->save($data);
    }

    //订单支付成功
    public function paySuccess(){
        if($this->status !=self::STATUS_UN_PAY){
            return;
        }
        $this->status = self::STATUS_PAY;
        $this->pay_time = time();
        $this->save();
        $type = substr($this->order_sn, 0, 2);
        switch ($type){
            //课程订单
            case self::COURSE_ORDER:
                $orderModel = new Order();
                $orderInfo = $orderModel->where("order_sn", $this->order_sn)
                    ->find();
                $user = \app\common\model\User::get($orderInfo["user_id"]);
                $orderInfo->payOrder($user, $this->pay_from, $this->pay_type);
                break;
            //充值余额
            case self::RECHARGE_MONEY:
                $user = \app\admin\model\User::get($this->user_id);
                $money = $this->total_fee;
                if($user['is_vip'] == 0){
                    $orderModel = new Order();
                    $course_count = $orderModel->where("user_id",$this->user_id)
                        ->where("status",">=", $orderModel::STATUS_PAY)
                        ->count();
                    $vip_low_money = \config("site.vip_low_money");
                    $buy_course_count = \config("site.buy_course_count");
                    if($money >= $vip_low_money && $course_count>= $buy_course_count){
                        $user->changeVip(1, $this->id);
                    }
                }
                \app\admin\model\User::money($money, $this->user_id , MoneyLog::MONEY , MoneyLog::RECHARGE_MONEY, '充值余额');
                break;
            //购买会员
            case self::SUPER_VIP:
                $user = \app\admin\model\User::get($this->user_id);
                $user->changeVip(1, $this->id);
                \app\admin\model\User::money($this->total_fee, $this->user_id , MoneyLog::MONEY , MoneyLog::BUY_VIP, '购买会员');
                break;
            //购买优惠券
            case self::BUY_COUPON:
                $couponModel = new \app\admin\model\course\Coupon();
                $info = $couponModel->getInfo([
                    "id"=>$this->data_id,
                ]);
                $user = \app\admin\model\User::get($this->user_id);
                $info->sendCoupon($user['id']);
                break;
        }
    }



    //生成订单号
    public static function getOrderSn($type){

        $order_id_main = $type . date('YmdHis') . rand(10000000,99999999);
        $order_id_len = strlen($order_id_main);
        $order_id_sum = 0;
        for($i=0; $i<$order_id_len; $i++){
            $order_id_sum += (int)(substr($order_id_main,$i,1));
        }
        $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
        return $osn;

    }

    //生成订单号
    public function changeRefundNo(){
        $this->refund_no = PayLog::getOrderSn(PayLog::REFUND);
        $this->save();
    }
}
